package com.ruoyi.system.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.BatchCard;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Card;
import com.ruoyi.system.service.ICardService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 礼品卡Controller
 * 
 * @author ruoyi
 * @date 2024-02-20
 */
@RestController
@RequestMapping("/system/card")
public class CardController extends BaseController
{
    @Autowired
    private ICardService cardService;

    /**
     * 查询礼品卡列表
     */
    @PreAuthorize("@ss.hasPermi('system:card:list')")
    @GetMapping("/list")
    public TableDataInfo list(Card card)
    {
        startPage();
        List<Card> list = cardService.selectCardList(card);
        return getDataTable(list);
    }

    /**
     * 导出礼品卡列表
     */
    @PreAuthorize("@ss.hasPermi('system:card:export')")
    @Log(title = "礼品卡", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Card card)
    {
        List<Card> list = cardService.selectCardList(card);
        ExcelUtil<Card> util = new ExcelUtil<Card>(Card.class);
        util.exportExcel(response, list, "礼品卡数据");
    }

    /**
     * 获取礼品卡详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:card:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cardService.selectCardById(id));
    }

    /**
     * 新增礼品卡
     */
    @PreAuthorize("@ss.hasPermi('system:card:add')")
    @Log(title = "礼品卡", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Card card)
    {
        return cardService.insertCard(card);
    }

    /**
     * 批量新增礼品卡
     */
    @PreAuthorize("@ss.hasPermi('system:card:batchAdd')")
    @Log(title = "礼品卡", businessType = BusinessType.INSERT)
    @PostMapping("/batchAdd")
    public AjaxResult batchAdd(@RequestBody BatchCard batchCard)
    {
        return cardService.insertBatchCard(batchCard);
    }

    /**
     * 修改礼品卡
     */
    @PreAuthorize("@ss.hasPermi('system:card:edit')")
    @Log(title = "礼品卡", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Card card)
    {
        return toAjax(cardService.updateCard(card));
    }

    /**
     * 删除礼品卡
     */
    @PreAuthorize("@ss.hasPermi('system:card:remove')")
    @Log(title = "礼品卡", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cardService.deleteCardByIds(ids));
    }

    /*
    * 每天0点 重置所有密码锁定状态
    * */
    @Scheduled(cron = "0 0 0 * * *")
//    @Scheduled(cron = "0 0 16 * * *")
    public void resetting(){
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");

        try {
            cardService.resetting();
            System.out.println(dateFormat.format(date) + "重置卡片锁定状态成功");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(dateFormat.format(date) + "重置卡片锁定状态失败");
        }
    }

}
